﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using Xeno.Importers;

namespace Xeno.Core
{
    public class ResourceImporter
    {
        public static List<ResourceImporter> Importers = new System.Collections.Generic.List<ResourceImporter>();

        public static bool RegisterStandardImporters()
        {
            RegisterImporter(new ColladaImporter());
            return true;
        }
        public static void RegisterImporter(ResourceImporter imp)
        {

            Importers.Add(imp);
          
        }
        public static ResourceImporter GetImporter(string path)
        {
            foreach (var imp in Importers)
            {
                if (imp.CanImport(path))
                {
                    return imp;
                }
            }
            return null;
        }
        public static XenoNode ImportResource(string path, XenoNode parent)
        {
            Console.WriteLine("Searching for importer for:" + path);
            var imp = GetImporter(path);
            if (imp != null)
            {
                Console.WriteLine("Importer found:" + imp.GetType().ToString());
                Console.WriteLine("About to import.");
                var node = imp.Import(path, parent);
                Console.WriteLine("Imported.");
                if (node == null)
                {
                    Console.WriteLine("Importer returned null resource.");
                    return null;
                }
                else
                {
                    Console.WriteLine("Importer returned valid resource.");
                    return node;
                }
            }
            else
            {
                Console.WriteLine("No importer found for:" + path);
                return null;

            }
        }
        public virtual bool CanImport(string path)
        {
            return false;
        }

        public virtual XenoNode Import(string path,XenoNode parent)
        {
            Console.WriteLine("Uhoh!");
            return null;
        }


    }

 
}
